package com.example.leetdemo.finash;

import java.util.Stack;

public class Solution48 {

    public void rotate(int[][] matrix) {

        for (int i = 0; i < matrix.length; i++) {
            for (int j = i; j < matrix.length; j++) {
                int t = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = t;
            }
        }

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length / 2; j++) {
                int t = matrix[i][j];
                matrix[i][j] = matrix[i][matrix[i].length - j - 1];
                matrix[i][matrix[i].length - j - 1] = t;
            }
        }


    }

    public boolean isValid(String s) {
        Stack<Character> characterStack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (characterStack.size() > 0) {
                if (s.charAt(i) == '}' && characterStack.peek() == '{') {
                    characterStack.pop();
                } else if (s.charAt(i) == ')' && characterStack.peek() == '(') {
                    characterStack.pop();
                } else if (s.charAt(i) == ']' && characterStack.peek() == '[') {
                    characterStack.pop();
                } else {
                    characterStack.push(s.charAt(i));
                }
            } else {
                characterStack.push(s.charAt(i));
            }
        }
        return characterStack.size() == 0;
    }

}
